package cn.xiaoxiang.org.controller;

import cn.xiaoxiang.basic.util.AjaxResult;
import cn.xiaoxiang.basic.util.LoginContext;
import cn.xiaoxiang.basic.util.PageList;
import cn.xiaoxiang.org.domain.Employee;
import cn.xiaoxiang.org.query.EmployeeQuery;
import cn.xiaoxiang.org.service.EmployeeService;
import cn.xiaoxiang.user.domain.Logininfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/emp")
@Api(value = "员工的API",description="员工相关的CRUD功能")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @GetMapping()
    public List<Employee> findAll(){
        return employeeService.findAll();
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询详情" )
    public Employee findById(@ApiParam(value="员工id",required = true) @PathVariable("id") Long id) {
        return employeeService.findById(id);

    }
    @GetMapping("/e/{id}")
    @ApiOperation(value = "通过ID查询详情" )
    public List<Employee> findEmp(@ApiParam(value="员工id",required = true) @PathVariable("id") Long id) {
        return employeeService.findemp(id);

    }
    //刪除
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        employeeService.delete(id);
        return AjaxResult.me().success();
    }

    //批量刪除
    @PatchMapping
    public AjaxResult batchdelete(@RequestBody List<Long> ids) {
        employeeService.batchdelete(ids);
        return AjaxResult.me().success();
    }
//添加或者修改
@PutMapping()
public AjaxResult addOrUpdate(@RequestBody Employee employee) {
    if (null != employee.getId()) {
        // 修改
        employeeService.update(employee);
    } else {
        // 添加
        employeeService.add(employee);
    }
    return AjaxResult.me().success();
}
    //分頁
    @PostMapping()
    public PageList<Employee> queryByPage(@RequestBody EmployeeQuery query) {

        return employeeService.queryByPage(query);
    }

}
